package com.itheima.controller;


import com.itheima.pojo.UmsMenu;
import com.itheima.pojo.UmsResource;
import com.itheima.pojo.UmsRole;
import com.itheima.service.RoleService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询  +  模糊查询
     */
    @GetMapping("list")
    public ResponseEntity list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String keyword){

        PageResultVo<UmsRole> prv = roleService.list(pageNum,pageSize,keyword);
        return ResponseEntity.ok(Result.success(prv));
    }

    /**
     * 添加角色
     */
    @PostMapping("create")
    public ResponseEntity create(@RequestBody UmsRole umsRole){
           roleService.create(umsRole);
           return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量删除角色
     */
    @PostMapping("delete")
    public ResponseEntity delete(@RequestParam ArrayList<Integer> ids){
           roleService.delete(ids);
        return ResponseEntity.ok(Result.success(null));
    }


    /**
     * 修改角色
     */
    @PostMapping("update/{id}")
    public ResponseEntity update(@PathVariable Integer id,@RequestBody UmsRole umsRole){
        roleService.update(id,umsRole);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 修改角色状态
     */
    @PostMapping("updateStatus/{id}")
    public ResponseEntity updateStatus(@PathVariable Integer id, @RequestParam Integer status){
        roleService.updateStatus(id,status);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 给角色分配菜单
     */
    @PostMapping("allocMenu")
    public ResponseEntity allocMenu(@RequestParam Integer roleId,@RequestParam ArrayList<Integer> menuIds){
        roleService.allocMenu(menuIds,roleId);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 给角色分配资源
     */
    @PostMapping("allocResource")
    public ResponseEntity allocResource(@RequestParam Integer roleId,@RequestParam ArrayList<Integer> resourceIds){
        roleService.allocResource(roleId,resourceIds);
        return ResponseEntity.ok(Result.success(null));
    }



    /**
     * 获取角色相关菜单
     */
    @GetMapping("listMenu/{roleId}")
    public ResponseEntity listMenu(@PathVariable Integer roleId){
        List<UmsMenu> menuList = roleService.listMenu(roleId);
        return ResponseEntity.ok(Result.success(menuList));
    }

    /**
     * 获取资源相关资源列表
     */
    @GetMapping("listResource/{roleId}")
    public ResponseEntity listResource(@PathVariable Integer roleId){
        List<UmsResource> resourceList = roleService.listResource(roleId);
        return ResponseEntity.ok(Result.success(resourceList));
    }

    /**
     * 查询所有
     */
    @GetMapping("/listAll")
    public ResponseEntity listAll(){
        List<UmsRole> umsRoleList = roleService.listAll();
        return ResponseEntity.ok(Result.success(umsRoleList));
    }


}
